
package model.contextlistener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import DBM.BuildDB;
import DBM.DBManager;
import dao.AccountDAO;
import dao.ActionDAO;
import dao.CharacterDAO;
import dao.EquipmentDAO;
import dao.HomeDAO;
import dao.LandDAO;
import dao.LogDAO;
import dao.MailDAO;
import dao.NetworkDAO;
import dao.ObjectDAO;

public class ContextListener implements ServletContextListener {
	@Override
	public void contextInitialized( ServletContextEvent SCE ) {
		ServletContext context = SCE.getServletContext();
		String dbLocation = context.getInitParameter( "dblocation" );
		String dbName = context.getInitParameter( "dbName" );
		String inlogName = context.getInitParameter( "inlogName" );
		String password = context.getInitParameter( "password" );
		DBManager DBM = new DBManager( dbLocation, dbName, inlogName, password );
		context.setAttribute( "DBM", DBM );
		EquipmentDAO equipmentDAO = new EquipmentDAO( DBM );
		HomeDAO homeDAO = new HomeDAO( DBM );
		MailDAO mailDAO = new MailDAO( DBM );
		LandDAO landDAO = new LandDAO( DBM );
		ObjectDAO objectDAO = new ObjectDAO( DBM );
		NetworkDAO networkDAO = new NetworkDAO( DBM );
		CharacterDAO characterDAO = new CharacterDAO( DBM, equipmentDAO,
				homeDAO, mailDAO, objectDAO, landDAO, networkDAO );
		ActionDAO actionDAO = new ActionDAO( DBM );
		AccountDAO accountDAO = new AccountDAO( DBM, characterDAO, actionDAO,
				mailDAO );
		LogDAO logDAO = new LogDAO( DBM );
		BuildDB bdb = new BuildDB( DBM );
		bdb.dropDB();
		bdb.buildDB();
		accountDAO.addAccount( "Kenny", "Kenneth Veenstra", "Trollface",
				"kenveenstra92@gmail.com" );
		characterDAO.setAdmin( 1 );
		SCE.getServletContext().setAttribute( "AccountDAO", accountDAO );
		SCE.getServletContext().setAttribute( "CharacterDAO", characterDAO );
		SCE.getServletContext().setAttribute( "ActionDAO", actionDAO );
		SCE.getServletContext().setAttribute( "LogDAO", logDAO );
		SCE.getServletContext().setAttribute( "MailDAO", mailDAO );
		SCE.getServletContext().setAttribute( "EquipmentDAO", equipmentDAO );
	}

	@Override
	public void contextDestroyed( ServletContextEvent SCE ) {
	}
}
